<?php
/**
 * Created by PhpStorm.
 * User: MrHung
 * Date: 8/10/14
 * Time: 7:15 PM
 */
session_start();
error_reporting(0);

/*
       * --------------------------
       *   Require/Include Files
       * -------------------------
       */
require_once('../../class_ajax_request/classAjax.php');
include_once('../../application/functions.php');
include_once('../../application/DataConfig.php');
require_once'../../vendor/facebook/facebook.php';
$obj = new AjaxRequest();
$rs = $obj->getSetting();

$app_id = $rs[0]['FB_APP_ID'];
$secret = $rs[0]['FB_APP_SECRET'];
$facebook = new Facebook(array(
    'appId'=>$app_id,
    'secret'=>$secret,
    'cookie'=>true
));

$user = $facebook->getUser();
$accessToken = $facebook->getAccessToken();

if ($user) {
    try {
        $post = $_POST;
        if(isset($post['askId'])){
            $resultPost = $obj->getOnePost($post['askId']);
            $urlShare = 'http://'.$_SERVER['HTTP_HOST'].'/ask/'.$post['askId'];
            ($resultPost[0]['photo'] != '') ? $photo = 'http://'.$_SERVER['HTTP_HOST'].'/upload/'.$resultPost[0]['photo'] : 'http://'.$_SERVER['HTTP_HOST'].'/public/img/logo.png';
            if(isset($post['fb_share']) && $post['fb_share'] == 'yes'){
            $result = $facebook->api(
            '/me/feed/',
            'post',
            array('message' => $resultPost[0]['post_details'],
                'picture' => $photo,
                'description'=> $resultPost[0]['post_details'],
                'link'=> $urlShare
            )
        );
              $result = (array(
                  'status'=>true,
                  'message'=>'Share on your wall Success!',
              ));
            }else{
                $result = (array(
                    'status'=>true,
                    'message'=>'Share on your wall Success!',
                ));
            }

        } else{
            $result = (array(
                'status'=>false,
                'message'=>'Share Error',
            ));
        }
        echo json_encode($result);die;
        $user_profile = $facebook->api('/me');

    }
    catch (Exception $e) {
        echo $e->getMessage();
        exit();
    }
}else{
    echo 'Wrong FB_APP_ID OR FB_SECRET';exit;
}
$post = $_POST;


